<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;

class Staff extends Model
{
    //

	use Notifiable;
	protected $table='staff';
	protected $fillable=['name','type','uid','avator','contract_number','phone','address','department',
		'station','card','opening_bank','company_id','status','job','task_status'
	];
	protected $hidden=[];

	public function lists($where){
		$res= $this->where(function ($query) use($where){
			$query->where(['company_id'=>$where['company_id']]);
			if($where['name'] !=null ){
				$query->where('name','like','%'.$where['name'].'%');
			}
			if($where['start_time'] !=null ){
				$query->where('created_at','>',$where['start_time']);
			}
			if($where['end_time'] !=null ){
				$query->where('created_at','<',$where['end_time']);
			}
			if($where['phone'] !=null ){
				$query->where('phone','like','%'.$where['phone'].'%');
			}
			if($where['status'] !=null ){
				$query->where(['job'=>$where['status']]);
			}
			if($where['status_'] !=null ){
				$query->where(['status'=>$where['status_']]);
			}
			if($where['department'] !=null ){
				$query->where(['department'=>$where['department']]);
			}
		})->with('task_status')->with('departments')->paginate(config('app.page'));

		return $res;
	}


//	查询员工的详细信息
	public function staff_desc($id){
		return $this->where(['id'=>$id])->first();
	}


//	查询任务状态
	public function task_status(){
		return $this->belongsTo('App\Model\Task_status','task_status');
	}


//	查询所属部门
	public function departments(){
		return $this->belongsTo('App\Model\Department','department');
	}

//	新增兼职员工的岗位
	public function staff_station($store_id,$staff_id,$store_station_id){
		$department_id=$this->department_id($store_id);

		$this->whereIn('id',$staff_id)->update(['department'=>$department_id,'station'=>$store_station_id]);
	}

//	查询该店铺的部门
	public function department_id($store_id){
		return Store::where(['id'=>$store_id])->first()->department_id;
	}


//	查询该部门所属的公司
	public function company_id($department_id){
		return Department::where(['id'=>$department_id])->first()->company_id;
	}
//	添加兼职员工
	public function addStaff_($uid,$store_id,$store_station_id){
		$department_id=$this->department_id($store_id);
		$company_id=$this->company_id($department_id);
		$res=$this->has_staff($uid);
		if($res){
			$res=$res->toArray();
			foreach($res as $kk=>$vv){
				$res['type']=2;
				$res['department']=$department_id;
				$res['station']=$store_station_id;
				$res['job']=1;
				$res['status']=1;
			}
		}else{
			$res['uid']=$uid;
			$res['type']=2;
			$res['department']=$department_id;
			$res['station']=$store_station_id;
			$res['job']=1;
			$res['status']=1;
			$res['company_id']=$company_id;
		}

		return $this->create($res)->id;
	}


//	添加接单员工
	public function addStaff($uid,$store_id,$store_station_id){
		$userData=Users::where(['id'=>$uid])->first();
		$data['uid']=$userData['id'];
		$data['avator']=$userData['avator'];
		$data['name']=$userData['realname'];
		$data['phone']=$userData['phone'];
		$data['department']=$userData['department'];
		$data['station']=$store_station_id;
		$data['company_id']=$userData['company_id'];
		$res=Staff::create($data);
		return $res->id;
	}



	public function has_staff($uid){
		return Staff::where(['uid'=>$uid])->first();
	}




//  业务组负责人
	public function department_staff($department_id){
		$data=$this->where(['department'=>$department_id,'type'=>1])
			->with('departments')->with('userinfo')->get();
		return $data;
	}

//	添加成员
	public function department_staffs($department_name,$group_id){
		if($group_id !=null){
			$user_idData=Group_member::where(['group_id'=>$group_id])->pluck('user_id');
		}
		$data=Department::where('name','=',$department_name)->first();
		if($data){
			$department_id=$data->id;
		}else{
			return null;
		}
		$data=$this->where(['department'=>$department_id,'type'=>1])
			->with('departments')->with(['userinfo' => function ($query) {
				$query->select(['id','name','realname']);
				$query->with(['roles' => function ($query) {
					$query->select(['display_name']);
				}]);
			}])->get();

		$array=[];
		foreach($data as $key=>$value){
			if(count($value['userinfo']['roles'])){
				$array[$key]=$value;
			}
		}
		return $array;
	}
//	编辑添加成员
	public function department_staffs_($department_name,$group_id){

		$user_idData=Group_member::where(['group_id'=>$group_id])->pluck('user_id');
		$data=Department::where('name','=',$department_name)->first();
		if($data){
			$department_id=$data->id;
		}else{
			return null;
		}
		$data=$this->where(['department'=>$department_id,'type'=>1])
			->with('departments')->with(['userinfo' => function ($query) {
				$query->select(['id','name','realname']);
				$query->with(['roles' => function ($query) {
					$query->select(['display_name']);
				}]);
			}])->whereNotIn('id',$user_idData)->get();
		return $data;
	}


//	用户账号信息
	public function userinfo(){
		return $this->belongsTo('App\Model\Users','uid');
	}

	public function user_role(){
		return $this->hasMany('App\Model\Role_user','user_id','uid');
	}

	public function role(){
		return $this->belongsTo('App\Model\Role','role_id');
	}

	public function store_station(){
		return $this->belongsTo('App\Model\Store_station','station');
	}
	public function store_stations(){
		return $this->belongsTo('App\Model\Store_station','station');
	}


//	获取店铺的所有员工
	public function allStaff($store_id){
		$department_id=Store::where(['id'=>$store_id])->first()->department_id;
		return $this->where(['department'=>$department_id,'job'=>1])->count();

	}
//	获取店铺所有的全职在职员工
	public function allFormal($store_id){
		$department_id=Store::where(['id'=>$store_id])->first()->department_id;
		return $data=$this->where(['department'=>$department_id,'type'=>1,'status'=>1,'job'=>1])->with('store_station')->get();
	}
	public function staff_task(){
		return $this->hasOne('App\Model\Staff_task','staff_id')->with('store_station');
	}


//	查询员工所属的店铺
	public function staff_store($staff_id){
		return $this->where(['id'=>$staff_id])->with('store')->first();
	}


	public function store(){
		return $this->belongsTo('App\Model\Store','department','department_id');
	}


	public function staff_leave(){
		return $this->hasOne('App\Model\Leave','staff_id');
	}

	public function update_staff($data){
		$max_date=max($data['time']);
		$min_date=min($data['time']);
		foreach ($data['desc'] as $kk=>$vv){
//			$staffData[$kk]['staff_id']=$vv['staff_id'];
			$staffData[$kk]['min_date']=$min_date;
			$staffData[$kk]['max_date']=$max_date;
			$this->where(['id'=>$vv['staff_id']])
				->update(['min_date'=>$min_date,'max_date'=>$max_date]);
		}
		return $max_settle_time=max($data['time']);
	}
}
